package cn.web.web_module.service.impl;

import cn.itcast.mapper.horsemanMapper;
import cn.itcast.mapper.orderItemMapper;
import cn.itcast.mapper.orderMapper;
import cn.itcast.pojo.horseman;
import cn.itcast.pojo.order;
import cn.itcast.pojo.orderItem;
import cn.itcast.util.DoubleUtils;
import cn.web.web_module.entity.OrderItemInfoOV;
import cn.web.web_module.service.GetRecyclableDeductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 18:00 2021/4/8
 * @Modified By:
 */
@Service
public class GetRecyclableDeductImpl implements GetRecyclableDeductService {

    @Resource
    private orderMapper orderMapper;

    @Resource
    private orderItemMapper orderItemMapper;

    @Resource
    private horsemanMapper horsemanMapper;

    @Override
    public List<OrderItemInfoOV> getGetRecyclableDeduct(Map<String,String> orderNumber) {
        order order = orderMapper.selectOrderNumber(orderNumber.get("orderNumber"));
        List<horseman> horsemen = horsemanMapper.selectHorsemanOpenId(orderNumber.get("horsemanOpenid"));
        double horsemanStarLevel = Double.parseDouble(horsemen.get(0).getHorsemanStarLevel());
        List<orderItem> orderItems = orderItemMapper.findOrderId(order.getId());
        List<OrderItemInfoOV> RecyclableDeducts = new ArrayList<>();
        orderItems.forEach((v->{
            OrderItemInfoOV orderItemInfoOV = new OrderItemInfoOV();
            orderItemInfoOV.setProductType(v.getProductType());
            orderItemInfoOV.setProductImg(v.getProductImg());
            orderItemInfoOV.setFinallyWeight(v.getFinallyWeight());
            orderItemInfoOV.setAmount(v.getAmount());
            orderItemInfoOV.setUnitPrice(v.getUnitPrice());
            if (v.getAmount()==null||v.getAmount()==0){
                orderItemInfoOV.setRecyclable(BigDecimal.valueOf(v.getFinallyWeight()*horsemanStarLevel).setScale(4,BigDecimal.ROUND_HALF_UP));
            }else {
                orderItemInfoOV.setRecyclable(BigDecimal.valueOf(v.getSum()*0.2).setScale(4,BigDecimal.ROUND_HALF_UP));
            }
            orderItemInfoOV.setSum(BigDecimal.valueOf(v.getSum()).add(orderItemInfoOV.getRecyclable()).setScale(4,BigDecimal.ROUND_HALF_UP));
            RecyclableDeducts.add(orderItemInfoOV);
        }));

        return RecyclableDeducts;
    }
}
